package com.rasou.jdk;


import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class JDKInvocationContext implements InvocationContext {

    private final Object target;

    private final Method method;

    private final Object proxy;

    private Object [] parameters;

    private final Map<String, Object> contextData;

    public JDKInvocationContext(Object target, Method method, Object proxy, Object[] parameters) {
        this.target = target;
        this.method = method;
        this.proxy = proxy;
        this.parameters = parameters;
        this.contextData = new HashMap<>();
    }
    @Override
    public Object getTarget() {
        return this.target;
    }

    @Override
    public Object getTimer() {
        return null;
    }

    @Override
    public Method getMethod() {
        return this.method;
    }

    @Override
    public Constructor<?> getConstructor() {
        Constructor<?> constructor = null;
        try {
            constructor = this.target.getClass().getConstructor();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return constructor;
    }

    @Override
    public Object[] getParameters() {
        return this.parameters;
    }

    @Override
    public void setParameters(Object[] params) {
        this.parameters = params;
    }

    @Override
    public Map<String, Object> getContextData() {
        return this.contextData;
    }

    @Override
    public Object proceed() throws Exception {
        return method.invoke(getTarget(), getParameters());
    }
}
